<?php


namespace App\Logic;

use App\Models\Borker;
use App\server\EsServer;
use App\server\RankServer;
use App\server\RedisServer;

/**
 * 经纪人信息逻辑层
 * Class AdminLogic
 * @package App\Logic
 * @ name:zf
 */
class BorkerLogic
{

    /**
     * Undocumented function
     * 列表
     * @param [type] $name
     * @param [type] $phone
     * @return void
     */
    public static function BorkerList($name, $phone, $paginate)
    {
        if (!empty($name)) {
            $where[] = ['name', 'like', "%$name%"];
            $index = 'search_index';
            $search = [
                'id' => time(),
                'name' => $name,

            ];
            (new EsServer())->EsAdd($index, $search);
            SeachLogic::SeachEsInsert($name);
            //将搜索记录存入redis
            RankServer::rank($name);
        }
        if (!empty($phone)) {
            $where[] = ['phone', 'like', "%$phone%"];
        }
        if (empty($paginate)) {
            $paginate = 10;
        }

        if(!isset($where)) {
            $redis=RedisServer::createRedisObject();
            $redis->select(7);
            $BorkerList= ['data'=>$redis->getAll()];
            if(empty($BorkerList)){
                if( empty($paginate)){
                    $paginate=10;
                }
                //self::BorkeRedis();
                $BorkerList=Borker::paginate($paginate);
            }

        }else{
            $BorkerList=Borker::list($where, $paginate);
        }
        return $BorkerList;
    }

    /**
     * Undocumented function
     * 详情
     * @param [type] $id
     * @return void
     */
    public static function BorkerDetail($vid){
        if(!empty($vid)){
           $id=$vid;
        }
        $redis=RedisServer::createRedisObject();
        $redis->select(7);
        $Borker=$redis->hGetone($id);
        if(empty($Borker)){
            $Borker=Borker::detail(['id'=>$id]);
            $data=$Borker->toArray();
            $redis->hSetone($data);
        }
        return $Borker;
    }

    /**
     * Undocumented function
     * 编辑信息
     * @param [type] $id
     * @return void
     */
    public static function BorkerSave($updata,$request){
        $request->validate([
            'phone' =>'required|regex:/^1[^0-2]\d{9}$/',
            'name' =>'required',
            'firm'=>'required',
        ]);
        //判断用户是创建还是修改
        if(empty($updata['id'])||!isset($updata['id']) ){
            return Borker::create($updata);
        }else{
            return Borker::change('id',$updata['id'],$updata);
        }
    }

    /**
     * Undocumented function
     * 删除
     * @param [type] $id
     * @return void
     */
    public static function BorkerDel( $vid)
    {
        if (!empty($vid)) {
            $id = $vid;
        }
        return Borker::del('id', $id);
    }
    /**
     * 经济人信息存入redis
     */
    public static function BorkeRedis(){
        $BorkeRedis= Borker::select('id','name','phone','firm','new_fang','two_fang','zu_fang','status')->get()->toArray();
        //return $BorkeRedis;
        $redis=RedisServer::createRedisObject();
        $redis->select(7);
        $redis->hSetAll($BorkeRedis);
    }
}
